/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.deployment.helper;

import org.eclipse.uml2.uml.Artifact;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Property;

import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Defines management of property.
 * 
 * @author Gilles Cannenterre
 * @since 18 janv. 07
 */
public class NodePropertyHelper extends com.cea.papyrus.diagram.classdiagram.helper.NodePropertyHelper {

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#canExecuteCreateElement(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateUMLElement(Element umlParent) {
		boolean canCreate = super.canCreateUMLElement(umlParent);
		if ( canCreate || (umlParent instanceof Artifact)) {
			return true;
		}
		return canCreate;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#addElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean addUMLElement(Element element, Element umlParent) {
		boolean isAdded = super.addUMLElement(element, umlParent);
		if ( ( isAdded == false) && (umlParent instanceof Artifact) ) {
			return ((Artifact)umlParent).getOwnedAttributes().add((Property)element);
		}
		return isAdded;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#createElement(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * @param name 
	 * 
	 * @return 
	 */
	@Override
	public  Element createUMLElement(String name,  Element umlParent){
		Property aProperty = (Property)super.createUMLElement(name, umlParent);
		if ( ( aProperty == null) && (umlParent instanceof Artifact) ) {
			aProperty=((Artifact)umlParent).createOwnedAttribute(name, null);
			aProperty.setIsUnique(false);
		}
		return aProperty;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#removeElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {
		boolean isRemoved = super.removeUMLElement(element, umlParent);
		if ( (isRemoved == false) && (umlParent instanceof Artifact) ) {
			return ((Artifact)umlParent).getOwnedAttributes().remove(element);
		}
		return isRemoved;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#canExecuteCreateCommand(com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */
	/**
	 * 
	 * 
	 * @param parent 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement parent){
		if (!(parent instanceof Diagram)) {//the parent must be a graphNode
	
			if(parent.getSemanticModel()==null){ // the parent must a Feature
				GraphElement gp=parent.getContainer();//look for GreatParent
	
				if (gp!=null){
					if (!(gp instanceof Diagram)) {//great parent is not a diagram
						if(gp.getSemanticModel()!=null){ //it is linked to an UML element
							Element elt= ((Uml1SemanticModelBridge)gp.getSemanticModel()).getElement();
	
							if ( canCreateUMLElement(elt) && (gp.getContained().get(0)==parent)){ //this is the first container of a classifier.
								return true;
							}
						} 
					}
				}
			}
		}
		return false;
	}
}
